﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Business.DataBusiness;
using Business;
using System.Threading;

namespace MuaBanDT
{
    public partial class BanHang : Form
    {
       Utilities getData = new Utilities();
        public BanHang()
        {
            InitializeComponent();
        }
         
        private void btnChiTietHD_Click(object sender, EventArgs e)
        {
            ChiTietHoaDonBan ChiTietHDBan = new ChiTietHoaDonBan();
            ChiTietHDBan.ShowDialog();
        }

        private void btnThoatBan_Click(object sender, EventArgs e)
        {
           this.Close();
            
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            try
            {
                MucDT a = new MucDT();
                this.dataGridViewTiemKiem.DataSource = a.TimKiemDienThoai(this.txtBoxTimDTBan.Text);
            }
            catch
            {
                MessageBox.Show("Khong co san pham nay trong kho");
            }
        }

        
        private void btnClear_Click(object sender, EventArgs e)
        {
            Xoa();
        }
        private void Xoa()
        {
            this.tBoxMaHDB.Text = "";
            this.cBoxMaNV.Text = "";
            this.tBoxTenKhachH.Text = "";
            this.tBoxThueXuat.Text = "";
            this.tBoxKhuyenMai.Text = "0";
            this.tBoxDiaChi.Text = "";
            this.tBoxTongTien.Text  = "0";
            this.dateTimePickerNgayBan.Value = DateTime.Now;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            HoaDonBan a = new HoaDonBan();
            try
            {
            
            a.MaHDB=getData.CreateID("HoaDonBan","HDB");
            a.TenKH = this.tBoxTenKhachH.Text;
            a.DiaChi = this.tBoxDiaChi.Text;
            a.MaNV = this.cBoxMaNV.Text;
            a.ThueXuat=float.Parse(this.tBoxThueXuat.Text);
            a.KhuyenMai = float.Parse(this.tBoxKhuyenMai.Text);
            a.TongTien = float.Parse(this.tBoxTongTien.Text);
            a.NgayBan= DateTime.Parse(this.dateTimePickerNgayBan.Value.ToShortDateString());
            tBoxMaHDB.Text = a.MaHDB;
            if (a.Insert() == 1)
            {
                MessageBox.Show("Lưu thành công", "Hóa Đơn Bán", MessageBoxButtons.OK);
            }
            }
           catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
         private void BanHang_Load(object sender, EventArgs e)
        {
            this.cBoxMaNV.Items.AddRange(getData.GetMaNV("BH"));
            this.tBoxTongTien.Text = "0";
            this.tBoxKhuyenMai.Text = "0";
        }

         private void tabControlBanHang_KeyPress(object sender, KeyPressEventArgs e)
         {
             if ((int)e.KeyChar == 13)
             {
                 btnTim_Click(sender, e);
             }
         }

         private void tBoxThueXuat_KeyPress(object sender, KeyPressEventArgs e)
         {
             string thapPhanString =
             Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
             char thapPhanChar = Convert.ToChar(thapPhanString);
             if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar))
             { }
             else if (e.KeyChar == thapPhanChar && tBoxThueXuat.Text.IndexOf(thapPhanString) == -1)
             { }
             else
             {
                 MessageBox.Show("Bạn chỉ được nhập số", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 e.Handled = true;
             }
             
         }

         private void tBoxKhuyenMai_KeyPress(object sender, KeyPressEventArgs e)
         {
             tBoxThueXuat_KeyPress(sender, e);
         }

         private void buttonXem_Click(object sender, EventArgs e)
         {
            if (this.radioButtHDB.Checked == true)
            {
                this.dataGridViewXemTT.DataSource = getData.GetDataTable("HoaDonBan");
            }
            else
                this.dataGridViewXemTT.DataSource = getData.GetDataTable("ChiTietHDB");
         }
    }
}
